package se.tinkerway.bomberman.view;

import glcore.graphics.GLGraphics;
import se.tinkerway.bomberman.domain.Bomberman;
import se.tinkerway.bomberman.domain.Position;
import se.tinkerway.bomberman.domain.Tile;
import se.tinkerway.bomberman.domain.TileMap;
import se.tinkerway.bomberman.domain.tile.IndestructableWall;

public class GLRenderer {
	private int size;
	
	private static float[] BLUE = new float[] { 0.0f, 0.0f, 1.0f, 1.0f };
	
	public GLRenderer(int size) {
		this.size = size;
	}
	
	private float translateHorizontal(GLGraphics g, double x) {
		return (float) x * g.getWidth() / size + 20;
	}
	
	private float translateVertical(GLGraphics g, double y) {
		return (float) y * g.getHeight() / size - 20;
	}
	
	private void drawSprite(GLGraphics g, double x, double y, Frame f) {
		float tileWidth = (float) g.getWidth() / size;
		float tileHeight = (float) g.getHeight() / size;
		
		x = translateHorizontal(g, x);
		y = translateVertical(g, y);
		
		g.fillRect((float) x, (float) y, tileWidth, tileHeight, BLUE);
	}
	
	public void renderTiles(GLGraphics g, TileMap map) {
		Tile[][] tiles = map.getTiles();
		
		for (int y = 0; y < map.getSize(); y++) {
			for (int x = 0; x < map.getSize(); x++) {
				Tile t = tiles[y][x];
				
				if (t instanceof IndestructableWall) {
					drawSprite(g, x, y, t.getFrame());
				}
			}
		}
	}
	
	public void renderBomberman(GLGraphics g, Bomberman bomberman) {
		Position position = bomberman.getPosition();
		float x = translateHorizontal(g, position.x-Bomberman.SIZE);
		float y = translateVertical(g, position.y-Bomberman.SIZE);
		float w = (float) g.getWidth() / size;
		float h = (float) g.getHeight() / size;
		
		g.fillRect(x, y, w, h, BLUE);
		System.out.println(position.x + ", " + position.y);
	}
}
